package com.exchange.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "params.kettle")
public class KettleHelper {

    //@Value("${params.kettle.db_host}")
    private static String db_host;
    //@Value("${params.kettle.db_name}")
    private static String db_name;
    //@Value("${params.kettle.db_port}")
    private static String db_port;
    //@Value("${params.kettle.db_username}")
    private static String db_username;
    //@Value("${params.kettle.db_password}")
    private static String db_password;
    //@Value("${params.kettle.conn_username}")
    private static String conn_username;
    //@Value("${params.kettle.conn_password}")
    private static String conn_password;

    // lh 2021-01-06 修改为单例模式获取环境配置
/*    public static KettleEnvironments getEnviroments(){
        return new KettleEnvironments(db_host, db_name, db_port, db_username, db_password, conn_username, conn_password);
    }*/

    /**
     * 单例模式获取环境配置
     * @return
     */
    public static KettleEnvironments getEnviroments(){
        KettleEnvironments environments = Singleton.INSTANCE.getInstance();
        // IP 或者 数据库名称 不匹配则重新生成对象返回(可追加条件判断)
        if(null != environments && (
                !db_host.equals(environments.getDb_host()) ||
                !db_name.equals(environments.getDb_name())
        )){
            environments = new KettleEnvironments(db_host,db_name,db_port,db_username,db_password,
                    conn_username,conn_password);
            Singleton.INSTANCE.setInstance(environments);
        }
        return environments;
    }

    private enum Singleton{
        INSTANCE;
        private KettleEnvironments singleCase;
        Singleton(){
            singleCase = new KettleEnvironments(db_host,db_name,db_port,db_username,db_password,
                    conn_username,conn_password);
        }
        public KettleEnvironments getInstance(){
            return singleCase;
        }
        public void setInstance(KettleEnvironments environments){
            singleCase = environments;
        }
    }

    public String getDb_host() {
        return db_host;
    }

    public void setDb_host(String db_host) {
        this.db_host = db_host;
    }

    public String getDb_name() {
        return db_name;
    }

    public void setDb_name(String db_name) {
        this.db_name = db_name;
    }

    public String getDb_port() {
        return db_port;
    }

    public void setDb_port(String db_port) {
        this.db_port = db_port;
    }

    public String getDb_username() {
        return db_username;
    }

    public void setDb_username(String db_username) {
        this.db_username = db_username;
    }

    public String getDb_password() {
        return db_password;
    }

    public void setDb_password(String db_password) {
        this.db_password = db_password;
    }

    public String getConn_username() {
        return conn_username;
    }

    public void setConn_username(String conn_username) {
        this.conn_username = conn_username;
    }

    public String getConn_password() {
        return conn_password;
    }

    public void setConn_password(String conn_password) {
        this.conn_password = conn_password;
    }

}
